package com.helloniu.controller {

import com.helloniu.fl.NCHandler;
import com.helloniu.fl.SOHandler;
import com.helloniu.fl.util.Tool;
import com.helloniu.model.MainPanelModel;
import flash.utils.ByteArray;
	
public class MainPanelController extends AbsController {
	public function MainPanelController() {
		super();
		
		model = new MainPanelModel();
	}
	
	public static function getIns():MainPanelController {
		if(controller == null) {
			controller = new MainPanelController();
		}

		return controller;
	}
	
	override public function initConnection(ancHandler:NCHandler, asoHandler:SOHandler, auserID:String):void {
		if(inited) {
			return;
		}

		inited = true;
		super.initConnection(ancHandler, asoHandler, auserID);
		this.soHandler.addChangeListener(statusChange);   
	}
	
	private function statusChange(asoHandler:SOHandler, pname:String = null, 
		oldValue:* = null, newValue:* = null):void 
	{
	    if(pname == null) {
	        return;
	    }
	
	    newValue = asoHandler.getProperty(pname);
	
		if(pname == "syncPanel") {
	        processSyncPanel(newValue);
	    }
	}
	
	private function processSyncPanel(obj:*):void {
		if(obj.userID == _userID) {
			return;
		}

		//var m:MainPanelModel = obj.model as MainPanelModel;
		var m:* = obj.model;

		if(m == null) {
			Tool.alert("===errrr==main panel controller==");
			return;
		}
		
		(model as MainPanelModel).setSelectedIndex(parseInt(m + ""));
		updateViews();
		// update view
	}
	
	public function syncMainPanel():void {
		var syncObj:Object = new Object();
		syncObj.userID = _userID;
		var sm:int = (model as MainPanelModel).getSelectedIndex();
		//var barr:ByteArray = new ByteArray();		
		syncObj.model = sm + "";

		soHandler.setProperty("syncPanel", syncObj);
	}
	
	private static var controller:MainPanelController;
}
}